//
//  XXFilterGroupView.swift
//  OfferManager
//
//  Created by IT.Bear on 2018/1/4.
//  Copyright © 2018年 OFFER. All rights reserved.
//

import UIKit
import SnapKit

protocol XXFilerGroupViewDelegate: class {
    func handleSortClick()
    func handleFilterClick()
}

class XXFilterGroupView: UIView ,XXSortViewDelegate {
    
    weak var delegate : XXFilerGroupViewDelegate?
    //pragma MARK: 懒加载
    lazy var sortView :XXSortView = {
        let view = XXSortView.init(frame: self.targetController.view.bounds, data: ["默认","最新"])
        view.delegate = self
        return view
    }()
    
    lazy var menuItem :UIView = {
        let menu = UIView.init(frame:CGRect(x:0,y:0,width:XXScreenWidth,height:44))
        return menu
    }()
    
    lazy var sortButton :UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.setTitle("排序", for: .normal)
        button.titleLabel?.textAlignment = .center
        button.backgroundColor = Color_White;
        button.setTitleColor(Color_666, for: UIControlState.normal)
        button.setTitleColor(Color_Red, for: UIControlState.selected)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.addTarget(self, action: #selector(XXFilterGroupView.handleSortClick(_:)), for: .touchUpInside)
        return button
    }()
    
    lazy var filterButton :UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.setTitle("筛选", for: .normal)
        button.titleLabel?.textAlignment = .center
        button.backgroundColor = Color_White;
        button.setTitleColor(Color_666, for: UIControlState.normal)
        button.setTitleColor(Color_Red, for: UIControlState.selected)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.addTarget(self, action: #selector(XXFilterGroupView.handleFilterClick(_:)), for: .touchUpInside)
        return button
    }()
    //MARK:-
    
    let targetController :UIViewController
    
    init(frame: CGRect ,targetController: UIViewController) {
        self.targetController = targetController
        super.init(frame: frame)
        loadSubView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK:布局
    func loadSubView() {
        self.addSubview(self.menuItem)
        self.menuItem.addSubview(self.sortButton)
        self.menuItem.addSubview(self.filterButton)
//        self.addSubview(self.translucentView)
        layout()
    }
    
    func loadSortView() {
        self.targetController.view.addSubview(self.sortView)
        self.sortView.snp.makeConstraints { (make) in
            make.left.equalTo(self.targetController.view.snp.left)
            make.right.equalTo(self.targetController.view.snp.right)
            make.top.equalTo(self.targetController.view.snp.top).offset(44+64)
            make.bottom.equalTo(self.targetController.view.snp.bottom)
        }
        
        self.targetController.view.bringSubview(toFront: self.sortView)
    }
    
    func layout() {
        self.sortButton.snp.makeConstraints{(make) -> Void in
            make.left.top.bottom.equalTo(0)
            make.width.equalTo(self.menuItem).dividedBy(2)
        }
        self.filterButton.snp.makeConstraints{(make) -> Void in
            make.right.top.bottom.equalTo(0)
            make.width.equalTo(self.sortButton)
        }
    }
    
    //MARK:-
    //MARK:delegate
    func handleSortViewDismiss() {
        self.sortButton.isSelected = false
    }
    
    func handleSortItemClick() {
        
    }

    //MARK:-
    //pragma MARK:处理点击排序事件
    @objc func handleSortClick(_ sender:UIButton) {
        if sender.isSelected {
            self.sortView.dismiss()
            sender.isSelected = false
        }else{
            sender.isSelected = true
            self.filterButton.isSelected = false
            self.loadSortView()
            delegate?.handleSortClick()
        }
    }
    
    //pragma MARK:处理点击筛选事件
    @objc func handleFilterClick(_ sender:UIButton) {
        sender.isSelected = true
        self.sortButton.isSelected = false
        delegate?.handleFilterClick()
    }
    
    
}
